package com.itown.expo.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import webframe.common.helper.ControllerHelper;

import com.itown.expo.controller.base.MyBaseCommandController;
import com.itown.expo.model.GoodsClearItem;
import com.itown.expo.model.PassageBill;

public class EntGoodsClearController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command)
			throws RuntimeException {
		GoodsClearItem item = (GoodsClearItem) command;
		Long declId = ControllerHelper.extractId(request, "declId");
		Long goodsId = ControllerHelper.extractId(request, "goodsId");

		List<PassageBill> passagelist = getPassageBills(item);

		if (item.getClearFlag().intValue() == 2
				|| item.getClearFlag().intValue() == 4) {
			// ClearFlag 暂存2 提交4
			getServiceContainer().getCheckBizService().updateEntGoodsClear(
					declId, goodsId, item, passagelist);
		}
		return new ModelAndView(new RedirectView("EntClearList.html?id="
				+ declId));
	}

	// 将passageNo转化为list
	public static List<PassageBill> getPassageBills(GoodsClearItem item) {
		List<PassageBill> list = new ArrayList<PassageBill>();
		String passageNo = item.getPassageNo();
		String[] sa = passageNo.split("\r\n");
		for (String s : sa) {
			PassageBill p = new PassageBill();
			p.setDeclNo(item.getDeclNo());
			p.setGoodsNo(item.getGoodsNo());
			p.setPassageNo(s);
			list.add(p);
		}
		return list;
	}
}